package managedbeans;

import entities.Forumblocks;
import entities.Forumposts;
import entities.Forumthreads;
import java.io.Serializable;
import java.util.ArrayList;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import langresources.ResourcesController;
import utils.dataaccess.DBInterface;


@ManagedBean(name="forumThreadsBean")
@SessionScoped
public class ForumThreadsBean extends GenericForumBean implements Serializable{

    private ArrayList<Forumthreads> threads;
    private Forumblocks selectedBlock;
    private String targetBlockName;

    public ForumThreadsBean() {
         currentBlock = getCurrentBlock();
         DBInterface.updateEventCounter();
    }

    public ArrayList<Forumthreads> getThreads() {
         return DBInterface.getBlocksThreads(getCurrentBlock());
    }

    public String showThread(Forumthreads thread){
         setCurrentThread(thread);
         return "/post.xhtml?faces-redirect=true";
    }

    public void moveThread(Forumthreads thread){
         Forumblocks targetBlock = DBInterface.getBlockByName(targetBlockName);
         if(targetBlock.getId()!=currentBlock.getId()){
             DBInterface.moveThread(thread,currentBlock,targetBlock);
         } else {
             showMessage(ResourcesController.extractResource("MESSAGE.WARNING"), ResourcesController.extractResource("COPY_TO_CURRENT_BLOCK"));
         }
    }

    public void removeThread(Forumthreads thread){
        ArrayList<Forumposts> posts = DBInterface.getPosts(thread);
        for(Forumposts post : posts){
            DBInterface.delete(post);
        }
        DBInterface.delete(thread);
    }

    public int getPostsNumber(Forumthreads thread){
        return DBInterface.getPosts(thread).size();
    }

    // GETTERS-SETTERS

    public Forumblocks getSelectedBlock() {
        return selectedBlock;
    }

    public void setSelectedBlock(Forumblocks selectedBlock) {
        this.selectedBlock = selectedBlock;
    }

    public String getTargetBlockName() {
        return targetBlockName;
    }

    public void setTargetBlockName(String targetBlock) {
        this.targetBlockName = targetBlock;
    }

}
